/**
 * Created by dywu on 2015/9/24.
 */
angular.module('plat.service.fileupload', ['ngResource'])

    .service('FileUpload', ['$http', '$q', function ($http, $q) {
        this.uploadFileToUrl = function (file) {
            var fd = new FormData();
            fd.append('file', file);
            var deferred = $q.defer();
            $http({
                method: "POST",
                url: uploadUrl,
                data: fd,
                headers: {'Content-Type': undefined},
                transformRequest: angular.identity
            }).success(function (data, status, headers, config) {
                deferred.resolve(data);
            }).error(function (data, status, headers, config) {
                deferred.reject(data);
            });
            return deferred.promise;
        }
    }])

    .service('fileUpload',['$http','$q' ,function ($http,$q) {
        var uploadUrl="http://inner.test.shuwang.info/common/fileupload/upload/1020";
        //图片上传
        this.setavatarurl = function(imgsrc,user){
            var src = "";
            var promise = this.uploadFileToUrl(imgsrc,user);
            return promise;
        }
        //图片上传
        this.uploadFileToUrl = function(imgsrc,file){
            // var fd = new FormData();
            // fd.append('file', file);
            // var deferred = $q.defer();
            //
            var postData = {
                filename: imgsrc,
                file: file,
            }
            var fd = new FormData();
            angular.forEach(postData, function(val, key) {
                fd.append(key, val);
            });
            var deferred = $q.defer();

            $http({
                method: "POST",
                url: uploadUrl,
                data: fd,
                headers: {'Content-Type': undefined},
                transformRequest: angular.identity
            }).success(function(data,status,headers,config){
                deferred.resolve(data);
            }).error(function(data,status,headers,config){
                deferred.reject(data);
            });
            return deferred.promise;
        }
    }])
;